﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace BudgetData
{
    public class Mod
    {
        public static int Executed(string sql)
        {
            using (BudgetEntities db = new BudgetEntities())
            {
                return db.Database.ExecuteSqlCommand(sql);
            }
        }
        public static T Add<T>(T m) where T : class
        {
            using (BudgetEntities db = new BudgetEntities())
            {
                db.Set<T>().Add(m);
                db.SaveChanges();
                return m;
            }
        }
        public static int Update<T>(T m) where T : class
        {
            using (BudgetEntities db = new BudgetEntities())
            {
                db.Entry(m).State = System.Data.Entity.EntityState.Modified;
                return db.SaveChanges();
            }
        }
        public static int Delete<T>(Expression<Func<T, bool>> where) where T : class
        {
            using (BudgetEntities db = new BudgetEntities())
            {
                var h = db.Set<T>().Where(where);
                db.Set<T>().RemoveRange(h);
                //db.Entry(entity).State = EntityState.Deleted;
                return db.SaveChanges();
            }
        }
        public static T GetModel<T>(Expression<Func<T, bool>> where) where T : class
        {
            using (BudgetEntities db = new BudgetEntities())
            {
                return db.Set<T>().FirstOrDefault(where);
            }
        }

        public static List<T> GetList<T>(Expression<Func<T, bool>> where) where T : class
        {
            using (BudgetEntities db = new BudgetEntities())
            {
               return db.Set<T>().Where(where).ToList();
            }
        }
        public static List<T> GetList<T>(Expression<Func<T, bool>> where, Func<T, object> order,int ordertype,int page,int pagesize,out int totalcount) where T : class
        {
            using (BudgetEntities db = new BudgetEntities())
            {
                if (ordertype == 1) {
                    var o = db.Set<T>().Where(where).OrderBy(order);
                    totalcount = o.Count();
                    return o.Skip((page - 1) * pagesize).Take(pagesize).ToList();
                }
                else
                {
                    var o = db.Set<T>().Where(where).OrderByDescending(order);
                    totalcount = o.Count();
                    return o.Skip((page - 1) * pagesize).Take(pagesize).ToList();
                }
            }
        }
        public static List<T> GetList<T>(string sql, int page, int pagesize, out int totalcount) where T : class
        {
            using (BudgetEntities db = new BudgetEntities())
            {
                var o = db.Set<T>().SqlQuery(sql);
                totalcount = o.Count();
                return o.Skip((page - 1) * pagesize).Take(pagesize).ToList();
            }
        }
        public static List<T> GetList<T>(string sql) where T : class
        {
            using (BudgetEntities db = new BudgetEntities())
            {
                var o = db.Set<T>().SqlQuery(sql).ToList();
                return o;
            }
        }
    }
}
